package com.itbour.service.authority.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.dao.authority.RoleInfoDao;
import com.itbour.dto.authority.BackFuncDto;
import com.itbour.dto.authority.RoleDto;
import com.itbour.dto.authority.RoleFuncDto;
import com.itbour.service.authority.RoleInfoService;

public class RoleInfoServiceImpl implements RoleInfoService {
	private RoleInfoDao roleInfoDao;

	public RoleInfoDao getRoleInfoDao() {
		return roleInfoDao;
	}

	public void setRoleInfoDao(RoleInfoDao roleInfoDao) {
		this.roleInfoDao = roleInfoDao;
	}

	@Override
	public void addRoleInfo(RoleDto roleDto) {
		Map<String, Object> parm=new HashMap<String, Object>();
		parm.put("role_meta", roleDto.getRole_meta());
		parm.put("role_type", roleDto.getRole_type());
		parm.put("name", roleDto.getName());
		parm.put("desc", roleDto.getDesc());
		parm.put("update_by", roleDto.getUpdate_by());
		roleInfoDao.addRoleInfo(parm);
	}

	@Override
	public Page pageListRoleInfo(Integer pageNum, Integer pageSize,
			Map<String, Object> parm) {
		/*
		 * pageNum 第几页 pageSize 一页几条数据 skipNum 起始位置Limit 0,10; totalSize 总共多少条数据
		 */
		Page page = null;
		int skipNum = JspPage.getStartOfPage(pageNum, pageSize);// 获取起始位置
		int totalSize = roleInfoDao.getRoleInfoCnt(parm);
		while (totalSize > 0 && totalSize <= skipNum) {
			skipNum = skipNum - pageSize;
			pageNum = pageNum - 1;
		}
		// 分页检索
		parm.put("pageSize", pageSize);
		parm.put("skipResults", skipNum);
		List<RoleDto> data = roleInfoDao.pageListRoleInfo(parm);
		page = new JspPage(skipNum, totalSize, pageSize, data);
		return page;
	}

	@Override
	public void updRoleInfo(RoleDto roleDto) {
		Map<String, Object> parm=new HashMap<String, Object>();
		parm.put("role_id", roleDto.getRole_id());
		parm.put("role_meta", roleDto.getRole_meta());
		parm.put("role_type", roleDto.getRole_type());
		parm.put("name", roleDto.getName());
		parm.put("desc", roleDto.getDesc());
		parm.put("update_by", roleDto.getUpdate_by());
		roleInfoDao.updRoleInfo(parm);
	}

	@Override
	public List<RoleDto> selectOneRoleInfo(Map<String, Object> parm) {
		return roleInfoDao.pageListRoleInfo(parm);
	}

	@Override
	public void delRoleInfo(Map<String, Object> parm) {
		roleInfoDao.delRoleInfo(parm);
	}

	public boolean sureEmpower(Map<String, Object> parm) {
		String strList1=String.valueOf(parm.get("strList1"));//权限二和权限一
		String strList3=String.valueOf(parm.get("strList3"));//权限一
		String strList2=String.valueOf(parm.get("strList2"));//权限一
		
		
		// 先获取 某个功能分类下的 所有功能ID 全部删掉
		String funIds = roleInfoDao.getAllFuncId(parm);
		if (funIds != null && (!"null".equals(funIds))){
			parm.put("funIds",funIds);
			roleInfoDao.delRolePower(parm);// 首先删除之前的所有然后 再添加
		
			if(strList1 != null && (!"null".equals(strList1))&&(!"".equals(strList1))){
				parm.put("checkId", strList1);
				List<BackFuncDto> list=roleInfoDao.listPowerFunc(parm);
				for (BackFuncDto backFuncDto : list) {
					Map<String, Object> map=new HashMap<String, Object>();
					map.put("desc", backFuncDto.getDesc());
					map.put("func_id",backFuncDto.getFunc_id());
					map.put("roleId", parm.get("roleId"));
					map.put("authority", 1);
					map.put("authority2", 1);
					map.put("update_by", parm.get("update_by"));
					roleInfoDao.sureEmpower(map);
				}
			}
			if(strList2 != null && (!"null".equals(strList2))&&(!"".equals(strList2))){
				parm.put("checkId", strList2);
				List<BackFuncDto> list=roleInfoDao.listPowerFunc(parm);
				for (BackFuncDto backFuncDto : list) {
					Map<String, Object> map=new HashMap<String, Object>();
					map.put("desc", backFuncDto.getDesc());
					map.put("func_id",backFuncDto.getFunc_id());
					map.put("roleId", parm.get("roleId"));
					map.put("authority", 0);
					map.put("authority2", 1);
					map.put("update_by", parm.get("update_by"));
					roleInfoDao.sureEmpower(map);
				}
			}
			if(strList3 != null && (!"null".equals(strList3))&&(!"".equals(strList3))){
				parm.put("checkId", strList3);
				List<BackFuncDto> list=roleInfoDao.listPowerFunc(parm);
				for (BackFuncDto backFuncDto : list) {
					Map<String, Object> map=new HashMap<String, Object>();
					map.put("desc", backFuncDto.getDesc());
					map.put("func_id",backFuncDto.getFunc_id());
					map.put("roleId", parm.get("roleId"));
					map.put("authority", 1);
					map.put("authority2", 0);
					map.put("update_by", parm.get("update_by"));
					roleInfoDao.sureEmpower(map);
				}
			}
			return true;
		} else {
			return false;
		}
	}

	@Override
	public List<RoleFuncDto> getFatherFunc(Map<String, Object> parm) {
		return roleInfoDao.getFatherFunc(parm);
	}

	@Override
	public List<RoleFuncDto> getTopInfo(Map<String, Object> parm) {
		return roleInfoDao.getTopInfo(parm);
	}
	
}
